﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Xml;
using System.IO;
using ChinaClipping.ClippingMaster.MyTools;
namespace ChinaClipping.ClippingMaster
{
    public partial class Form_Update : Form
    {
        private bool _canceled = false;
        public Form_Update()
        {
            InitializeComponent();
        }

        private void Form_Update_Load(object sender, EventArgs e)
        {
            if (Uri.IsWellFormedUriString(Properties.Settings.Default.ClientInfoUrl, UriKind.Absolute))
            {
                WebClient wc = new WebClient();
                wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
                wc.Encoding = Encoding.UTF8;
                Uri clienturl = new Uri(Properties.Settings.Default.ClientInfoUrl);
                textBox1.AppendText("正在更新客户信息...");
                wc.DownloadStringAsync(clienturl);
            }
            else
                this.Close();
        }

        private void updateUserList()
        {
            if (Uri.IsWellFormedUriString(Properties.Settings.Default.UserInfoUrl, UriKind.Absolute))
            {
                WebClient wc2 = new WebClient();
                wc2.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted1);
                wc2.Encoding = Encoding.UTF8;
                Uri userurl = new Uri(Properties.Settings.Default.UserInfoUrl);
                textBox1.AppendText("正在更新用户信息...");
                wc2.DownloadStringAsync(userurl);
            }
        }

        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (this._canceled)
                return;
            try
            {
                string xmlstr = e.Result;
                XmlDataDocument xml = new XmlDataDocument();
                xml.LoadXml(xmlstr);
                //xml.Save(MySettings.ClientInfoFile);
                Wrapper w = new Wrapper(MySettings.KEY);
                string enstr = w.EncryptString(xmlstr);
                File.WriteAllText(MySettings.ClientInfoFile, enstr, Encoding.UTF8);
                textBox1.AppendText("完成。");
                textBox1.AppendText(Environment.NewLine);
            }
            catch (Exception ex)
            {
                textBox1.AppendText(string.Format ("失败。（{0}）",ex.Message));
                textBox1.AppendText(Environment.NewLine);
            }
            finally
            {
                updateUserList();
            }
        }

        void wc_DownloadStringCompleted1(object sender, DownloadStringCompletedEventArgs e)
        {
            if (this._canceled)
                return;
            try
            {
                string xmlstr = e.Result;
                XmlDataDocument xml = new XmlDataDocument();
                xml.LoadXml(xmlstr);
                //xml.Save(MySettings.ClientInfoFile);
                Wrapper w = new Wrapper(MySettings.KEY);
                string enstr = w.EncryptString(xmlstr);
                File.WriteAllText(MySettings.UserListFile, enstr, Encoding.UTF8);
                textBox1.AppendText("完成。");
                this.Close();
            }
            catch (Exception ex)
            {
                textBox1.AppendText(string.Format("失败。（{0}）", ex.Message));
            }
            finally
            {
                this.Close();
            }

        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this._canceled = true;
            this.Close();
        }

    }
}
